package com.ruoyi.dm.service.impl;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.*;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.dm.domain.*;
import com.ruoyi.dm.mapper.DataCollectionMapper;
import com.ruoyi.dm.mapper.DataManagementMapper;
import com.ruoyi.dm.service.DataCollectionService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.utils.DealDataUtils;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.janino.IClass;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.Function;

@Service
public class DataCollectionServiceImpl implements DataCollectionService {
    private static Map<String, SseEmitter> sseCache = new ConcurrentHashMap<>();
    @Resource
    private DataManagementMapper dataManagementMapper;
    @Resource
    private DataCollectionMapper dataCollectionMapper;
    @Value("${picture.path}")
    private String piPath;
    private final String directory = "data/";

    @Value("${python.dm.density.path}")
    private String densityPath;
    @Value("${python.dm.density.name}")
    private String densityName;
    @Value("${python.windDeal.path}")
    private String windPath;
    @Value("${python.output}")
    private String output;
    @Value("${python.pending}")
    private String pending;
    @Value("${python.dm.density.jsonPath}")
    private String jsonPath;
//    @Value("${python.test.path}")
//    private String pyPath;
//    @Value("${python.test.name}")
    private String pyName;

    @Override
    public AjaxResult add(MultipartFile[] files, DataCollectionParam param) {
        Date date = new Date();
        param.setCreateTime(date);
        //根据id获取表信息
        DataManagementParam dataManagementParam = dataManagementMapper.getInfo(param.getDataId());
        TableInfoParam tableInfoParam = JSON.parseObject(dataManagementParam.getTableStructure(),TableInfoParam.class);
        if (!tableInfoParam.getTableName().toUpperCase().contains("FILE")){
            tableInfoParam.setTableName(tableInfoParam.getTableName()+"_FILE");
        }
        param.setDataSource(dataManagementParam.getDataSource());
        //进行文件上传
        List<DataCollectionParam> listMap = uploadFile(files,param,directory);
        //根据dataId获取该资料下的数据模版
        String templatePath = dataCollectionMapper.getTemplatePath(param.getDataId());
        //首先进行数据格式校验
        Boolean flag = dataFormatCheck(listMap.get(0).getFilePath(), templatePath);
        if (!flag){
            //进行上传文件删除
            return AjaxResult.success("请检查文件导入格式");
        }
        String command = "";
        String json = jsonPath;
        File file;
        switch (param.getDataId().toString()){
            case "1":
                Map<String, Object> map = new HashMap<>();
                long timestamp = Instant.now().toEpochMilli();
                map.put("file_path",listMap.get(0).getFilePath());
                map.put("output_path",output);
                map.put("threshold_density","2.3");
                map.put("threshold_altitude","1.2");
                //进行json文件生成
                file = FileUtils.createJson(json+timestamp+".json",map);
                //拼装python执行命令
                command = "python "+densityPath+densityName+" "+file.getPath();
            case "2":
                //拼装python执行命令
                command = "python "+densityPath+densityName+" ";
        }
        String result = ExecUtil.execCmdWithResult(command);
        //进行数据解析
        //根据资料id获取数据表名称
        //进行数据入库保存
        dataCollectionMapper.add(listMap,tableInfoParam.getTableName());
        return AjaxResult.success("资料新增成功");
    }

    @Override
    public AjaxResult getCatalogList(Long parentId) {
        List<String> reList = dataCollectionMapper.getCatalogList(parentId);
        return AjaxResult.success("获取成功!",reList);
    }

    @Override
    public AjaxResult getSourceList(DataManagementParam param) {
        List<DataCollectionParam> reList = dataCollectionMapper.getSourceList(param);
        Iterator<DataCollectionParam> it = reList.iterator();
        while(it.hasNext()){
            DataCollectionParam param1 = it.next();
            DataManagementParam dataManagementParam = dataManagementMapper.getInfo(param1.getId());
            TableInfoParam tableInfoParam = JSON.parseObject(dataManagementParam.getTableStructure(),TableInfoParam.class);
            //查看是否存在表  不存在则移除
            int count = dataManagementMapper.isTable((tableInfoParam.getTableName()).toUpperCase());
            if (count < 1){
                it.remove();
            }
        }
        return AjaxResult.success("获取成功!",reList);
    }

    @Override
    public void download(List<DataCollectionParam> list, HttpServletResponse response) {
        List<String> fileUrls = new ArrayList<>();
        for (DataCollectionParam param : list ) {
            fileUrls.add(param.getFilePath());
        }
        ZipUtils.zipFile(fileUrls,response);
    }

    @Override
    public AjaxResult deleteDiskFile(Map<String, Object> map) {
        //调用磁盘文件删除方法
        deleteFile(map.get("filePath")+"");
        return AjaxResult.success("磁盘文件删除成功");
    }

    @Override
    @Transactional(rollbackFor = InterruptedException.class)
    @Async
    public AjaxResult test(List<String> nums,String id) {

        //根据文件数量进行线程数设置
        int corePoolSize;
        int maximumPoolSize;
        if (nums.size()<10){
            corePoolSize = nums.size();
            maximumPoolSize = corePoolSize+1;
        }else {
            corePoolSize = 10;
            maximumPoolSize = 2*corePoolSize;
        }
        ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize,maximumPoolSize,0L, TimeUnit.MILLISECONDS,new LinkedBlockingDeque<>());
        List<String> commandList = new ArrayList<>();
        commandList.add("python");
        //给调用python 命令中加上参数（-u）代表-u代表不通过缓存，直接输出,或者python代码print 结束后执行刷新缓冲区操作sys.stdout.flush()
        commandList.add("-u");
        commandList.add("D:\\test.py");
        Future<?> future = null;
        List<String> asList = new ArrayList<>();
        SseEmitter sseEmitter = sseCache.get(id);

        for (String ss :nums) {
            future = executor.submit(() -> {
                //组装python命令
//                String  commands = "python -u "+pyPath+pyName;
//                String results = ExecUtil.execPython(commandList,null);
                asList.add(Thread.currentThread().getName());
            });
        }

        try {
            if (future!=null){
                future.get();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            executor.shutdown();
        }
//        sseEmitter.complete();
        sseCache.remove(id);

        return AjaxResult.success(asList);
    }

    public static String execPython(List<String> commandList, SseEmitter sseEmitter) {
        ProcessBuilder processBuilder = new ProcessBuilder(commandList);
        try {
            Process process = processBuilder.start();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
            String packReadLine = "";
            while (null != (packReadLine = bufferedReader.readLine()) || process.isAlive()) {
                if (sseEmitter != null) {
                    try {
                        sseEmitter.send(SseEmitter.event().name("msg").data("后端发送消息：" + packReadLine));
//                return AjaxResult.success("aaaa");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(packReadLine);
            }
            bufferedReader.close();
            process.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    public AjaxResult importData(String path,Integer nums) {
//        //分区性能测试
//        String testSql = "SELECT lat,lon,alt,time,density FROM STANDARD_DEN_GA PARTITION (SYS_P1258_1343) WHERE TIME>='2006-01-01 00:00:52' and TIME<='2006-04-01 00:00:00'";
//        String testSql1 = "SELECT lat,lon,alt,time,density FROM ROOT.STANDARD_DEN_GA  WHERE TIME>='2006-01-01 00:00:52' and TIME<='2006-04-01 00:00:00'";
        String testSql2 = "SELECT * FROM SWDATA_OBS_DEN_CHAMP  WHERE TIME>='2001-05-04 11:59:42' and TIME<='2010-09-04 20:05:12'";
//        String testSql3 = "SELECT lat,lon,alt,time,density FROM ROOT.STANDARD_DEN_GA_COPY  PARTITION (SYS_P1476_1486) WHERE TIME>='2006-01-01 00:00:52' and TIME<='2006-04-01 00:00:00'";
//        String testSql4 = "SELECT lat,lon,alt,time,density FROM ROOT.STANDARD_DEN_GA_COPY  WHERE TIME>='2006-01-01 00:00:52' and TIME<='2006-04-01 00:00:00'";
//
//        Long start = System.currentTimeMillis();
        List<Map<String, Object>> resultList = new ArrayList<>();
//        resultList = dataCollectionMapper.executeSql1(testSql);
//        System.out.println("分区表查询数据耗时：  "+(System.currentTimeMillis()-start));
//        resultList.clear();
//        System.gc();
//        Long start1 = System.currentTimeMillis();
//        resultList = dataCollectionMapper.executeSql1(testSql1);
//        System.out.println("普通表查询数据耗时：  "+(System.currentTimeMillis()-start1));
//        resultList.clear();
//        System.gc();
        Long start2 = System.currentTimeMillis();
        resultList = dataCollectionMapper.executeSql1(testSql2);
        System.out.println("分区表不指定分区查询数据耗时：  "+(System.currentTimeMillis()-start2));
        resultList.clear();
        System.gc();
//        Long start3 = System.currentTimeMillis();
//        resultList = dataCollectionMapper.executeSql1(testSql3);
//        System.out.println("分区表指定分区全局索引查询数据耗时：  "+(System.currentTimeMillis()-start3));
//        resultList.clear();
//        System.gc();
//        Long start4 = System.currentTimeMillis();
//        resultList = dataCollectionMapper.executeSql1(testSql4);
//        System.out.println("分区表指定不指定分区全局索引查询数据耗时：  "+(System.currentTimeMillis()-start4));
//        resultList.clear();
//        System.gc();

//        File folder = new File(path);
//        DealDataUtils dealDataUtils = new DealDataUtils();
//        dealDataUtils.dealForeignDensity(folder,nums);
        return null;
    }

    @Override
    public AjaxResult importDc(String path, Integer nums) {
        File folder = new File(path);
        DealDataUtils dealDataUtils = new DealDataUtils();
        dealDataUtils.dealDcData(folder,nums);
        return null;
    }

    @Override
    public SseEmitter push(String id,String flag){
        return SseEmitterUtils.connect(id, flag);
//        // 超时时间设置为3s，用于演示客户端自动重连
//        SseEmitter sseEmitter = new SseEmitter(0L);
//        // 设置前端的重试时间为1s
//        try {
//            sseEmitter.send(SseEmitter.event().reconnectTime(1000).data("连接成功"));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        sseCache.put(id, sseEmitter);
//        System.out.println("add " + id);
//        sseEmitter.onTimeout(() -> {
//            System.out.println(id + "超时");
//            sseCache.remove(id);
//        });
//        sseEmitter.onCompletion(() -> System.out.println("完成！！！"));
//        return sseEmitter;
    }

    @Override
    public void downloadTemplate(Long dataId,HttpServletResponse response) {
        //根据资料id进行模版文件存储路径下载
        String filePath = dataCollectionMapper.getTemplatePath(dataId);
        File file = new File(filePath);
        BufferedOutputStream bos = null;
        BufferedInputStream bis = null;
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setHeader(
                "Content-disposition",
                "attachment; filename="+file.getName());
        try{
            bis = new BufferedInputStream(new FileInputStream(file));
            // 输出流
            bos = new BufferedOutputStream(response.getOutputStream());
            byte[] buff = new byte[1024];
            int len;
            while ((len = bis.read(buff)) > 0) {
                bos.write(buff, 0, len);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public AjaxResult importAtmWindField(MultipartFile[] files, AtmWindFieldParam param, MultipartFile stationFile) {
        StringBuilder builder = new StringBuilder("python"+" "+windPath);
        String inPath = pending;
        String rePath = output;
        String tableName = "";
        String sounding = "";
        if (stationFile!=null){
            //获取文件名
            String fileName = stationFile.getOriginalFilename();
            File directoryPath = new File(inPath+"SOUNDING");
            //判断文件路径是否存在
            if (!directoryPath.exists()) {
                directoryPath.mkdirs();//创建目录
            }
            //拼装文件上传地址
            sounding = inPath+"SOUNDING/"+fileName;
            File dest = new File(sounding);
            //判断文件是否存在
            if (!dest.exists()) {
                try {
                    stationFile.transferTo(dest);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        switch (param.getType()){
            case "Nimbus7":
                inPath +="Nimbus7";
                tableName = "SWDATA_OBS_WIND_NIMBUS_FILE";
                builder.append("Nimbus7_analysis.py ").append(dealTime(param.getStartTime())).append(" ").append(dealTime(param.getEndTime())).append(" ").append(inPath+" ").append(rePath+"Nimbus7");
                break;
            case "TIMED":
                inPath +="TIMED";
                tableName = "SWDATA_OBS_WIND_TIMED_FILE";
                builder.append("TIMED_analysis.py ").append(dealTime(param.getStartTime())).append(" ").append(dealTime(param.getEndTime())+" ").append(inPath+" ").append(rePath+"TIMED ").append(param.getFileType()+" ").append(param.getInterMethod());
                break;
            case "UARS":
                inPath +="UARS";
                tableName = "SWDATA_OBS_WIND_UARS_FILE";
                builder.append("UARS_analysis.py ").append(dealTime(param.getStartTime())).append(" ").append(dealTime(param.getEndTime())+" ").append(inPath+" ").append(rePath+"UARS ").append(param.getFileType()+" ").append(param.getInterMethod());
                break;
            case "SOUNDING"://无线电探空
                inPath +="SOUNDING";
                tableName = "SWDATA_OBS_WIND_SOUNDING_FILE";
                builder.append("SOUNDING_analysis.py ").append(dealTime(param.getStartTime())).append(" ").append(dealTime(param.getEndTime())+" ").append(inPath+" ").append(rePath+"SOUNDING ").append(sounding+" ").append(param.getHour()+" ").append(param.getInterMethod());
                break;
            case "cRadar":
                inPath +="RADAR";
                tableName = "SWDATA_OBS_WIND_CRADAR_FILE";
                builder.append("RADAR_MH_analysis.py ").append(dealTime(param.getStartTime())).append(" ").append(inPath+" ").append(rePath+"RADAR ").append(dealTime(param.getEndTime())+" ").append(param.getStationName());
                inPath +="/MH";
                break;
            case "fRadar":
                inPath +="RADAR";
                tableName = "SWDATA_OBS_WIND_FRADAR_FILE";
                builder.append("RADAR_BATH_analysis.py ").append(dealTime(param.getStartTime())).append(" ").append(inPath+" ").append(rePath+"RADAR ").append(dealTime(param.getEndTime())+" ").append(param.getStationName());
                inPath +="/BATH";
                break;
            case "fSatellite":
                inPath +="SATELLITE_STANDARD_WIND";
                tableName = "SWDATA_OBS_WIND_SATELLITE_FILE";
                builder.append("GRACE_CHAMP_SWARM_analysis.py ").append(param.getStartTime().replaceAll("-","")).append(" ").append(param.getEndTime().replaceAll("-","")+" ").append(inPath+" ").append(rePath+" ").append(param.getDataType()).append(" ").append(param.getInterMethod());
                inPath+="/STANDARD_WIND_"+param.getDataType();
                break;
            case "UKMO":
                String[] arg = param.getResolution().split("x");
                inPath +="UKMO";
                tableName = "SWDATA_OBS_WIND_UKMO_FILE";
                builder.append("UKMO_analysis.py ").append(param.getStartTime().replaceAll("-","")).append(" ").append(param.getEndTime().replaceAll("-","")+" ").append(inPath+" ").append(rePath+"UKMO ").append(" [").append(arg[0]+","+arg[1]+","+arg[2]+"] ").append(param.getInterMethod());
                break;
        }
        for (MultipartFile file : files) {
            //获取文件名
            String fileName = file.getOriginalFilename();
            if (param.getType().equals("Nimbus7") || param.getType().equals("TIMED")|| param.getType().equals("UKMO") || param.getType().equals("cRadar") || param.getType().equals("fRadar")){
                inPath += "/"+param.getStartTime().substring(0,4);
            }
            File directoryPath = new File(inPath);
            //判断文件路径是否存在
            if (!directoryPath.exists()) {
                directoryPath.mkdirs();//创建目录
            }
            //拼装文件上传地址
            String filePath = inPath+"/"+fileName;
            File dest = new File(filePath);
            //判断文件是否存在
            if (!dest.exists()) {
                try {
                    file.transferTo(dest);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        System.out.println(builder.toString());
        String result = ExecUtil.execCmdWithResult(builder.toString());
//        String result = "###C:\\Users\\zys\\Desktop\\1111.txt#20210101###C:\\Users\\zys\\Desktop\\1111.txt#20210102###";
        System.out.println(result);
        if (result.contains("没有符合条件")){
            return AjaxResult.warn("数据格式不符合处理条件");
        }
        String[] arg = result.split("###");
//        System.out.println(arg[0]+" "+arg[1]+arg[2]);
        Date date = new Date();
        List<DataCollectionParam> list = new ArrayList<>();
        List<Date> timeList = new ArrayList<>();
        Collections.sort(timeList, (a, b) -> a.compareTo(b));
        StringBuilder timeBuilder = new StringBuilder();
        for (String path : arg) {
            if (path.contains("#")){
                String[] args = path.split("#");
                File file = new File(args[0].replaceAll("\n",""));
                DataCollectionParam dataCollectionParam = new DataCollectionParam();
                //进行数据入库处理
                if(!file.exists()){
                    System.out.println("文件为空");
                }
                dataCollectionParam.setFileType(file.getName().substring(file.getName().lastIndexOf(".")).split("\\.")[1]);
                dataCollectionParam.setFileSize(new BigDecimal((double) file.length() / (1024 * 1024)).setScale(2, RoundingMode.UP) + "MB");
//              如果为雷达数据特殊处理时间
                if (param.getType().contains("Radar")){
                    dataCollectionParam.setEndTime(DateUtils.convertString2Date("yyyy-MM-dd",DateUtils.getNormalDateTimeStr(args[2])));
                }else {
                    dataCollectionParam.setEndTime(DateUtils.convertString2Date("yyyy-MM-dd HH:mm:ss",DateUtils.getNormalDateTimeStr(args[1])+" 23:59:59"));
                }
                dataCollectionParam.setStartTime(DateUtils.convertString2Date("yyyy-MM-dd HH:mm:ss",DateUtils.getNormalDateTimeStr(args[1])+" 00:00:00"));
                dataCollectionParam.setCreateTime(date);
                dataCollectionParam.setFileName(file.getName());
                dataCollectionParam.setFilePath(args[0]);
                dataCollectionParam.setDataId(SequenceBuilder.sequenceId());
                list.add(dataCollectionParam);
                timeList.add(dataCollectionParam.getStartTime());
                timeBuilder.append("'").append(DateUtils.getNormalDateTimeStr(args[1])+" 00:00:00").append("'").append(",");
            }
        }
        //进行数据是否存在判断，存在即更新，否则直接插入
        List<Map<String, Object>> idList = dataCollectionMapper.getIdList(tableName, (timeBuilder.deleteCharAt(timeBuilder.length() - 1)).toString(),
                DateUtils.convertDate2String("yyyy-MM-dd HH:mm:ss", timeList.get(0)),DateUtils.convertDate2String("yyyy-MM-dd HH:mm:ss", timeList.get(timeList.size()-1)));
        Map<String, Object> idMap = new HashMap<>();
        for (Map<String, Object> map :idList ) {
            idMap.put(map.get("START_TIME")+"_"+map.get("END_TIME"), map.get("ID"));
        }
        if (idMap != null && !idMap.isEmpty()){
            for (DataCollectionParam param1 :list) {
                if(idMap.containsKey(param1.getStartTime()+"_"+param1.getEndTime())){
                    param1.setId(Long.valueOf(idMap.get(param1.getStartTime()+"_"+param1.getEndTime())+""));
                    param1.setUpdateTime(new Date());
                    System.out.println("shujugengxin1");
                    dataCollectionMapper.updateAtmWindField(param1);
                }
            }
        }else {
            System.out.println("charu");
            int count =0;
            String table = tableName;
            Function<List<?>, Integer> function1= x->dataCollectionMapper.insertWindBatch((List<Map<String, Object>>)x, table);
            count += DatabaseUtil.splitInsert(list, function1,1000);
        }
        return AjaxResult.success("数据解析成功！");
    }

    /**
     * 分页查询
     */
    @Override
    public AjaxResult selectList(DataCollectionParam param) {
        //获取所有需要查询列表中的所有表名
        List<String>  list = dataCollectionMapper.getTableStructure(param);
        //拼接所有查询条件
        StringBuilder ifBuilder = new StringBuilder();
        //添加生产时间范围条件
        if (param.getStartTime()!=null && param.getEndTime()!=null){
            ifBuilder.append(" and start_time >= '").append(DateUtils.convertDate2String("yyyy-MM-dd HH:mm:ss", param.getStartTime())).append("'");
            ifBuilder.append(" and end_time <='").append(DateUtils.convertDate2String("yyyy-MM-dd HH:mm:ss", param.getEndTime())).append("'");
        }
        //添加数据来源条件
        if (!StringUtils.isBlank(param.getDataSource())){
            ifBuilder.append(" and data_source like concat('%','").append(param.getDataSource()).append("','%')");
        }
        //添加数据类型条件
        if (!StringUtils.isBlank(param.getFileType())){
            ifBuilder.append(" and file_type='").append(param.getFileType()).append("'");
        }
        //查询所有表下满足条件的数据
        StringBuilder sqlBuilder = new StringBuilder();
        for (String tableStructure : list) {
            TableInfoParam tableInfoParam = JSON.parseObject(tableStructure,TableInfoParam.class);
            //首先判断表名是否为空或者数据库是否存在表
            //判断是否存在该表
            int count = dataManagementMapper.isTable((tableInfoParam.getTableName()).toUpperCase());
            if (StringUtils.isBlank(tableInfoParam.getTableName())|| count == 0){
                continue;
            }
            //进行遍历拼接所有满足条件的数据
            String tableName = tableInfoParam.getTableName().toUpperCase();
            if (!tableName.contains("FILE")){
                tableName += "_FILE";
            }
            sqlBuilder.append("select * ,(CASE WHEN id>0 THEN '").append(tableName+"'").append(" ELSE 0 END) as table_name from ").append(tableName);
            //进行查询条件添加
            if (ifBuilder.length()!=0){
                sqlBuilder.append(" where");
                sqlBuilder.append(ifBuilder.toString().replaceFirst("and"," "));
            }
            sqlBuilder.append(" union ");
        }
        List<DataCollectionParam> reList = new ArrayList<>();
        if (sqlBuilder.toString().equals("")){
            return  AjaxResult.success("获取成功",reList,0);
        }
        //首先进行数据总数的获取
        Integer nums =  dataCollectionMapper.countSql(sqlBuilder.substring(0,sqlBuilder.toString().length()-6));
        if (nums ==0){
            return AjaxResult.success("获取成功",reList,nums);
        }
        //计算分页查询起始行号
        int pageStartIndex = 0;
        if (nums>param.getPageSize()){
            pageStartIndex = (param.getPageNum()-1)*param.getPageSize();
        }
        reList = dataCollectionMapper.selectList(sqlBuilder.substring(0,sqlBuilder.toString().length()-6)+"limit "+pageStartIndex+","+param.getPageSize());
        return AjaxResult.success("获取成功",reList,nums);
    }

    /**
     * 编辑资料
     */
    @Override
    public AjaxResult edit(DataCollectionParam param) {
        Date date = new Date();
        param.setUpdateTime(date);
        //根据资料id获取数据表名称
        //进行数据入库更新
        dataCollectionMapper.edit(param);
        return AjaxResult.success("修改成功");
    }

    /**
     * 删除
     */
    @Override
    public AjaxResult delete(List<DataCollectionParam> list) {
        for (DataCollectionParam param : list) {
            //首先进行文件删除
            deleteFile(param.getFilePath());
            //在进行表中记录删除
            dataCollectionMapper.delete(param.getId(), param.getTableName());
        }
        return AjaxResult.success("资料删除成功!");
    }

    public  List<DataCollectionParam> uploadFile(MultipartFile[] files, DataCollectionParam params, String directory) {
        List<DataCollectionParam> list = new ArrayList<>();
        //判断文件是否存在
        if (files != null && files.length > 0) {
//            String prefix = PictureProperties.getUrl();
            //进行文件上传
            for (MultipartFile file : files) {
                DataCollectionParam param = new DataCollectionParam();
                param.setDataId(params.getDataId());
                param.setCreateTime(params.getCreateTime());
                param.setDataSource(params.getDataSource());
                param.setFrequency(params.getFrequency());
                param.setOrFileName(params.getOrFileName());
                param.setStartTime(params.getStartTime());
                param.setEndTime(params.getEndTime());
                //获取文件名
                String fileName = file.getOriginalFilename();
                String fileType = fileName.substring(fileName.lastIndexOf(".")).split("\\.")[1];
                File directoryPath = new File(piPath + directory);
                //判断文件路径是否存在
                if (!directoryPath.exists()) {
                    directoryPath.mkdirs();//创建目录
                }
                //拼装文件上传地址
                String filePath = piPath + directory + fileName;
                File dest = new File(filePath);
                //判断文件是否存在
                if (!dest.exists()) {
                    try {
                        file.transferTo(dest);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("生成文件");
                //处理压缩文件
                if (fileType.equals("zip")) {
                    //解压文件夹
                    try {
                        List<Map<String, Object>> mapList = ZipUtils.unzip(piPath + directory + fileName, piPath + directory);
                        if (mapList!=null && mapList.size()>0){
                            //添加压缩包中文件信息
                            for (Map<String, Object> map :mapList) {
                                DataCollectionParam param1 = new DataCollectionParam();
                                param1.setDataId(params.getDataId());
                                param1.setDataSource(params.getDataSource());
                                param1.setFrequency(params.getFrequency());
                                param1.setOrFileName(params.getOrFileName());
                                param1.setStartTime(params.getStartTime());
                                param1.setEndTime(params.getEndTime());
                                param1.setCreateTime(params.getCreateTime());
                                param1.setFilePath(map.get("filePath")+"");
                                param1.setFileSize(map.get("fileSize")+"");
                                param1.setFileName(map.get("fileName")+"");
                                param1.setFileType(map.get("fileType")+"");
                                param.setId(SequenceBuilder.sequenceId());
                                list.add(param1);
                            }
                        }
                        //进行压缩包删除
                        deleteFile(piPath + directory + fileName);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }else {
                    String fileSize = new BigDecimal((double) file.getSize() / (1024 * 1024)).setScale(2, RoundingMode.UP) + "MB";
                    //添加文件信息
                    param.setFilePath(filePath);
                    param.setFileSize(fileSize);
                    param.setFileName(fileName);
                    param.setFileType(fileType);
                    param.setId(SequenceBuilder.sequenceId());
                    list.add(param);
                }
                param = null;
            }
        }
        return list;
    }

    public static void deleteFile(String filePath) {
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();
            System.gc();
        }
    }
    private Boolean dataFormatCheck(String path, String templatePath){
        File file = new File(path);
        InputStreamReader read = null;
        BufferedReader bufferedReader = null;
        try {
            read = new InputStreamReader(new FileInputStream(file), "UTF-8");
            bufferedReader = new BufferedReader(read);
            String str = bufferedReader.readLine();
            read = null;
            bufferedReader = null;
            read = new InputStreamReader(new FileInputStream(templatePath), "UTF-8");
            bufferedReader = new BufferedReader(read);
            String strTemplate = bufferedReader.readLine();
            if (str.equalsIgnoreCase(strTemplate)){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (read != null) {
                    read.close();
                }
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return false;
    }
    private String dealTime(String time){
        //首先按照"-"进行拆分
        String[] arg = time.replaceAll("-0", "-").split("-");
        String reString = "("+arg[0]+","+arg[1];
        if (arg.length>2){
            reString += ","+arg[2];
        }
        reString+=")";
        return reString;
    }

}

